package durak.server.model;

import java.util.ArrayList;
import java.util.List;

import durak.common.model.Card;
import durak.common.model.Cards;
import durak.common.model.CardsOnTheTable;
import durak.common.model.CommonPlayerData;
import durak.common.model.DurakDeck;
import durak.common.model.GameTableState;

// we need to do : init from XML file ( read data from XML file to init the game)
/**
 * GamingTable class holds the current state of the table before each action the
 * server draws information from this class and updates the new state after each
 * action
 */

public class GamingTable {

	private DurakDeck deck;
	private List<Player> playersList;
	private boolean isGameStillRunning;
	private Player currentAttacker;
	private Player coAttacker;
	private Player currentDefender;

	private Card kozar;
	private Cards bita;
	private CardsOnTheTable cardsOnTheTable;

	/**
	 */
	public GamingTable() {

		playersList = new ArrayList<Player>();
		deck = new DurakDeck();
		bita = new Cards();
		cardsOnTheTable = new CardsOnTheTable();
		// randomize deck
	}

	// GamingTable ();

	/**
	 * @return the cardsOnTheTable
	 */
	public CardsOnTheTable getCardsOnTheTable() {
		return cardsOnTheTable;
	}

	/**
	 * @param cardsOnTheTable
	 *            the cardsOnTheTable to set
	 */
	public void setCardsOnTheTable(CardsOnTheTable cardsOnTheTable) {
		this.cardsOnTheTable = cardsOnTheTable;
	}

	/**
	 * @return the currentAttacker
	 */
	public Player getCurrentAttacker() {
		return currentAttacker;
	}

	/**
	 * @param currentAttacker
	 *            the currentAttacker to set
	 */
	public void setCurrentAttacker(Player currentAttacker) {
		this.currentAttacker = currentAttacker;
	}

	public Player getCoAttacker() {
		return coAttacker;
	}

	public void setCoAttacker(Player coAttacker) {
		this.coAttacker = coAttacker;
	}

	/**
	 * @return the deck
	 */
	public DurakDeck getDeck() {
		return this.deck;
	}

	/**
	 * @param deck
	 *            the deck to set
	 */
	public void setDeck(DurakDeck deck) {
		this.deck = deck;
	}

	/**
	 * @return the playersList
	 */
	public List<Player> getPlayersList() {
		return playersList;
	}

	/**
	 * @param playersList
	 *            the playersList to set
	 */
	public void setPlayersList(List<Player> playersList) {
		this.playersList = playersList;
	}

	/**
	 * @return the isGameStillRunning
	 */
	public boolean isGameStillRunning() {
		return isGameStillRunning;
	}

	/**
	 * @param isGameStillRunning
	 *            the isGameStillRunning to set
	 */
	public void setGameStillRunning(boolean isGameStillRunning) {
		this.isGameStillRunning = isGameStillRunning;
	}

	/**
	 * @return the currentDefender
	 */
	public Player getCurrentDefender() {
		return currentDefender;
	}

	/**
	 * @param currentDefender
	 *            the currentDefender to set
	 */
	public void setCurrentDefender(Player currentDefender) {
		this.currentDefender = currentDefender;
	}

	/**
	 * @return the kozar
	 */
	public Card getKozar() {
		return kozar;
	}

	/**
	 * @param kozar
	 *            the kozar to set
	 */
	public void setKozar(Card kozar) {
		this.kozar = kozar;
	}

	/**
	 * @return the bita
	 */
	public Cards getBita() {
		return bita;
	}

	/**
	 * @param bita
	 *            the bita to set
	 */
	public void setBita(Cards bita) {
		this.bita = bita;
	}

	public void addPlayer(Player player) {
		playersList.add(player);

	};

	public Player getNextPlayer(Player attacker) {
		int index = playersList.indexOf(attacker);

		if (index == -1)
			return null;
		if (index + 1 >= playersList.size())
			index = -1;

		return playersList.get(index + 1);
	}

	public void updateGameTableState(GameTableState gameState) {
		gameState.setCardsOnTheTable(cardsOnTheTable);
		gameState.setCurrentAttacker(currentAttacker.getName());
		gameState.setCurrentDefender(currentDefender.getName());
		gameState.setGameStillRunning(isGameStillRunning);
		gameState.setKozar(kozar);
		gameState.setDeckSize(deck.size());
		gameState.getPlayers().clear();
		for (Player player : playersList) {
			gameState.getPlayers().add(new CommonPlayerData(player.isPlaying(), player.getName(), player.getLoses(), player.getHand().size()));
		}
	}
	
	public void resetGameTable()
	{
		for(Player player : playersList) {
			player.getHand().removeAll();
			if (!player.isLeftTheGame())
				player.setPlaying(true);
		}
		deck = new DurakDeck();
		bita = new Cards();
		cardsOnTheTable = new CardsOnTheTable();
		currentAttacker = null;
		coAttacker = null;
		currentDefender = null;
		kozar = null;
	}

	public Player getNextAttacker() {
		Player nextAttacker = currentDefender;
		while(!nextAttacker.isPlaying() && !nextAttacker.getName().equals(currentAttacker.getName())) {
			nextAttacker = getNextPlayer(nextAttacker);
		}
		return nextAttacker;
	}
}
